package com.resou.reader.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.view.Loading;
import com.resou.reader.view.NetErrorFragment;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.jessyan.autosize.internal.CustomAdapt;

public abstract class ResouBaseActivity<P extends ResouBasePresenter> extends AppCompatActivity implements CustomAdapt {

    protected P mPresenter;
    protected Unbinder unbinder;
    private NetErrorFragment mNetErrorFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initStatusBar();
        initPresenter();
        initView();
        if (mPresenter != null) {
            mPresenter.subscribe();
        }
        PushAgent.getInstance(this).onAppStart();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unsubscribe();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
        dissLoading();
    }

    protected abstract void initStatusBar();

    protected abstract void initPresenter();

    protected abstract void initView();

    public abstract int getLayoutId();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 重新请求数据
     */
    public abstract void retryRefreshData();

    /**
     * 显示网络异常视图
     *
     * @param resId
     */
    public void showNetErrorView(int resId) {
        if (mNetErrorFragment == null) {
            mNetErrorFragment = new NetErrorFragment(new NetErrorFragment.OnClick() {
                @Override
                public void onClick() {
                    if (NetworkUtils.isNetworkConnected(ResouBaseActivity.this)) {
                        dismissNetErrorView();
                        retryRefreshData();
                    } else {
                        ToastUtil.makeShortToast("网络异常");
                    }

                }
            });
        }
        mNetErrorFragment.show(resId, getSupportFragmentManager());
    }

    /**
     * 关闭无网络页码
     */
    public void dismissNetErrorView() {
        if (mNetErrorFragment != null) {
            mNetErrorFragment.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 显示进度条
     */
    public void showLoading() {
        if (!Loading.getInstance().isShow() && !Loading.getInstance().isRemoving() && !Loading.getInstance().isVisible() && !Loading.getInstance().isAdded()) {
            Loading.getInstance().show(getSupportFragmentManager());
        }
    }

    /**
     * 自定义显示加载圈布局
     */
    public void setLoadingView(int view, String message) {
        Loading.getInstance().setView(view);
        Loading.getInstance().setLoadingText(message);
    }

    /***
     * 取消加载
     */
    public void dissLoading() {
        if (Loading.getInstance().isShow()) {
            Loading.getInstance().dismiss();
        }
    }

    /**
     * 是否又网络
     *
     * @return
     */
    public boolean isNetwork() {
        if (NetworkUtils.isNetworkConnected(this)) {
            return true;
        } else {
            ToastUtil.makeShortToast("网络异常");
            return false;
        }
    }

    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    @Override
    public float getSizeInDp() {
        return 375;
    }


}
